<template>
  <div>
    <editForm :formConfig="formConfig" ref="editForm" :formData="editFormData">
    </editForm>
  </div>
</template>

<script>
import {
  getTableConfigApi, //获取表格过滤器数据
  setTableConfigApi, //更新表格过滤器数据
} from "@/api/common";
import { getLocations } from "@/api/operationManagement/handoverManagement.js";
export default {
  data() {
    return {
      pageCode: "handover",
      formConfig: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "值班单位",
            prop: "deptName",
            idsStr: "deptId",
            type: "dept", //类型
            span: 8,
            disabled: true,
          },
          {
            label: "值班人员类型",
            prop: "staffType",
            type: "select",
            span: 8,
            disabled: false,
            selectOption: this.$store.getters.dirtData("operation_staff_type"),
            methods: (val) => {
              this.$refs.editForm.setFormItem("staffTypes", [val.value]);
              this.setTableConfig();
            },
          },
          {
            label: "值班地点",
            prop: "locationId",
            type: "select",
            selectOption: [],
            span: 8,
            methods: (data, formData) => {
              this.setTableConfig();
              localStorage.removeItem("workStationOnduty");
              localStorage.setItem("workStationOnduty", "workStationOnduty");
            },
          },
        ],
      },
      //弹出框表单数据
      editFormData: {},
      filterObj: {},
    };
  },
  created() {
    this.getlocaltionList();
    this.getTableConfig();
  },
  methods: {
    //获取查询条件
    async getTableConfig() {
      let option = await this.getlocaltionList();
      let form = {
        page: this.pageCode,
        userId: this.$store.state.user.userInfo.id,
      };
      getTableConfigApi(form).then((res) => {
        let data = res.data;
        this.userConfig = data;
        if (data.length == 0) {
          this.$set(
            this.editFormData,
            "deptId",
            this.$store.getters.currentOrgInfo.id
          );
          this.$set(
            this.editFormData,
            "deptName",
            this.$store.getters.currentOrgInfo.name
          );
        
        } else {
          data.map((i) => {
            let formData = {};
            if (i.type == "filter") {
              this.filterObj = i;
              formData = JSON.parse(i.config) || {};
            }
            formData.deptId = this.$store.getters.currentOrgInfo.id;
            formData.deptName = this.$store.getters.currentOrgInfo.name;
            let haveItem = option.find((item) => {
              return item.value == formData.locationId;
            });
            if (!haveItem) {
              formData.locationId = "";
            }
            this.editFormData = formData;
          });
        }
        this.$emit("getShiftInfo", this.editFormData);
      });
    },
    //保存查询条件
    setTableConfig() {
      let formData = this.$refs.editForm.getCurrentData();
      let form = {
        config: "",
        type: "filter",
        id: "",
        page: this.pageCode,
        userId: this.$store.state.user.userInfo.id,
      };
      form.id = this.filterObj.id;
      form.config = JSON.stringify(formData);
      setTableConfigApi(form)
        .then((res) => {
          this.$emit("getShiftInfo", formData);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    //获取查询条件
    async getFormData() {
      let data = await this.$refs.editForm.getFormData();
      return data;
    },
    //查询指定单位的值班地点
    getlocaltionList() {
      return new Promise((resolve, reject) => {
        let params = {
          deptId: this.$store.state.user.userInfo.orgId,
        };
        getLocations(params)
          .then((res) => {
            let option = res.data.map((item) => {
              let obj = {
                label: item.locationName,
                value: item.locationId,
              };
              return obj;
            });
            this.$set(this.formConfig.itemArr[2], "selectOption", option);
            resolve(option);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>